package com.smart.oil.service.impl;

import com.smart.oil.entity.SmRolePermission;
import com.smart.oil.exception.BusinessException;
import com.smart.oil.exception.code.BaseResponseCode;
import com.smart.oil.mapper.SmRolePermissionMapper;
import com.smart.oil.service.RolePermissionService;
import com.smart.oil.vo.req.RolePermissionOperationReqVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @ClassName: RolePermissionServiceImpl
 * TODO:类文件简单描述
 * @Author: 杨鑫鑫
 * @UpdateUser: 杨鑫鑫
 * @Version: 0.0.1
 */
@SuppressWarnings("ALL")
@Service
public class RolePermissionServiceImpl implements RolePermissionService {
    @Autowired
    private SmRolePermissionMapper smRolePermissionMapper;
    @Override
    public void addRolePermission(RolePermissionOperationReqVO vo) {
        smRolePermissionMapper.removeByRoleId(vo.getRoleId());
        if(vo.getPermissionIds()==null||vo.getPermissionIds().isEmpty()){
            return;
        }
        List<SmRolePermission> list=new ArrayList<>();
        for (String permissionId:
             vo.getPermissionIds()) {
            SmRolePermission smRolePermission=new SmRolePermission();
            smRolePermission.setId(UUID.randomUUID().toString());
            smRolePermission.setCreateTime(new Date());
            smRolePermission.setRoleId(vo.getRoleId());
            smRolePermission.setPermissionId(permissionId);
            list.add(smRolePermission);
        }
        int i = smRolePermissionMapper.batchInsertRolePermission(list);
        if(i==0){
            throw new BusinessException(BaseResponseCode.OPERATION_ERROR);
        }
    }

    @Override
    public List<String> getRoleIdsByPermissionId(String permissionId) {
        return smRolePermissionMapper.getRoleIdsByPermissionId(permissionId);
    }

    @Override
    public int removeRoleByPermissionId(String permissionId) {
        return smRolePermissionMapper.removeByPermissionId(permissionId);
    }

    @Override
    public List<String> getPermissionIdsByRoleId(String roleId) {
        return smRolePermissionMapper.getPermissionIdsByRoleId(roleId);
    }

    @Override
    public int removeByRoleId(String roleId) {
        return smRolePermissionMapper.removeByRoleId(roleId);
    }
    @Override
    public List<String> getPermissionIdsByRoles(List<String> roleIds) {

        return smRolePermissionMapper.getPermissionIdsByRoles(roleIds);
    }

}
